// $codepro.audit.disable emptyMethod, methodChainLength, numericLiterals, hidingInheritedFields
/**
 * License under GNU's GPL 
 * Nurse.java 
 */

import java.io.IOException;
import java.util.Scanner;
import java.util.logging.FileHandler;
import java.util.logging.Filter;
import java.util.logging.Level;
import java.util.logging.LogRecord;
import java.util.logging.Logger;

/**
 * Class represents a nurse
 * 
 * @author Team 9
 * @version 1.0
 * 
 */
public class Nurse extends User {
	/**
	 * nurse GUI
	 */
	private NurseGui window;
	
	/**
	 * type of object
	 */
	private int type;
	
	/**
	 * Logger variable
	 */
	public static String Fn;

	/**
	 * Logger variable
	 */
	public static String Ln;

	/**
	 * Logger variable
	 */
	// Logging code
	private static final Logger NURSELOGGER = Logger.getLogger("Nurse");

	static {
		FileHandler filehand = null;
		try {
			filehand = new FileHandler("Nurse.log");
		} catch (SecurityException se) {
			NURSELOGGER.log(Level.SEVERE,
					"Security Exception creating the logger", se);
			se.printStackTrace();
		} catch (IOException ioe) {
			NURSELOGGER.log(Level.SEVERE, "IO Exception creating the logger",
					ioe);
			ioe.printStackTrace();
		}

		filehand.setFilter(new Filter() {
			public boolean isLoggable(LogRecord record) {
				return true;
			}
		});
		NURSELOGGER.addHandler(filehand);
		NURSELOGGER.setLevel(Level.ALL);
	}// end logging code

	/**
	 * Overrides the toString() method to print the appropriate instance
	 * variables
	 * 
	 * @return String to be printed
	 */
	@Override
	public String toString() {
		return "Nurse , lastName=" + lastName + ", firstName=" + firstName
				+ ", userName=" + userName + ", suspend=" + suspend + ", ]";
	}

	/**
	 * Empty constructor for the class
	 */
	public Nurse() {
	}

	/**
	 * Constructor for the class
	 * 
	 * @param first
	 *            value for firstName
	 * @param last
	 *            value for lastName
	 * @param user
	 *            value for userName
	 * @param pass
	 *            value for password
	 */
	public Nurse(String first, String last, String user, String pass) {
		lastName = last;
		firstName = first;
		userName = user;
		password = pass;
		suspend = false;
		type = 2;
		NURSELOGGER.info("Nurse constructed: " + first + " " + last);
	}

	/**
	 * Method to create a new Patient object
	 */
	public void createPatient() {
		final Scanner scan = new Scanner(System.in);
		System.out.println("Enter first name");
		final String first = scan.nextLine();
		System.out.println("Enter last name");
		final String last = scan.nextLine();
		System.out.println("Enter user name");
		final String user = scan.nextLine();
		System.out.println("Enter password name");
		final String pass = scan.nextLine();

		final Patient patient = new Patient(first, last, user, pass);
		Gui.addToPatientArray(patient);
		NURSELOGGER.log(Level.INFO, "Patient created: " + first + " " + last);

	}

	/**
	 * Method to lookup an existing patient
	 * 
	 * @param first
	 *            first name of the patient
	 * @param last
	 *            last name of the patient
	 * @return index of the found patient or negative value, if not found
	 */
	public int lookup(String first, String last) {
		int counter = 0;
		while (counter < Gui.getPatientArray().size()) {
			if ((Gui.getPatientArray().get(counter).getFirstName()
					.compareTo(first) == 0)
					&& (Gui.getPatientArray().get(counter).getLastName()
							.compareTo(last) == 0)) {
				// display patient
				// counter++;
				break;
			} else {
				counter++;
			}
		}
		if (counter == Gui.getPatientArray().size()) {
			return -counter;
		} else {
			return counter;
		}
	}

	/**
	 * Method to edit a patient
	 * 
	 * @param first
	 *            first name of the patient to edit
	 * @param last
	 *            last name of the patient to edit
	 */
	public void editPatient(String first, String last) {
		final int index = lookup(first, last);
		if (index < 0) {
			System.out.println("Patient not found");
		} else {
			final Scanner scan = new Scanner(System.in);
			System.out.println("Enter first name");
			final String firstName = scan.nextLine();
			System.out.println("Enter last name");
			final String lastName = scan.nextLine();
			System.out.println("Enter user name");
			final String userName = scan.nextLine();
			System.out.println("Enter password name");
			final String pass = scan.nextLine();

			Gui.getPatientArray().get(index).setLastName(lastName);
			Gui.getPatientArray().get(index).setFirstName(firstName);
			Gui.getPatientArray().get(index).setUserName(userName);
			Gui.getPatientArray().get(index).setPassword(pass);

		}
		NURSELOGGER.log(Level.CONFIG, "Nurse " + Fn + " " + Ln
				+ " edited Patient: " + first + " " + last);
	}

	/**
	 * Method to delete a patient
	 * 
	 * @param first
	 *            first name of the patient to delete
	 * @param last
	 *            last name of the patient to delete
	 */
	public void delPatient(String first, String last) {
		final int index = lookup(first, last);
		// System.out.println(Gui.getPatientArray().get(index).getLastName());
		if (index >= 0) {
			Gui.getPatientArray().remove(index);
		} else {
			System.out.println("Patient not found");
		}
		NURSELOGGER.log(Level.CONFIG, "Nurse " + Fn + " " + Ln
				+ " removed Patient: " + first + " " + last);
	}

	/**
	 * Method to generate a GUI for the nurse
	 * 
	 */
	@Override
	public void showGUI() {
		window = new NurseGui();
		window.getFrame().setVisible(true);
	}

	/**
	 * Method to generate the invoice
	 * 
	 * @param first
	 *            first name of patient
	 * @param last
	 *            last name of the patient
	 */
	public void generateInvoice(String first, String last) {
		final int index = lookup(first, last);
		if (index < 0) {
			System.out.println("Patient not found");
		} else {
			final int treatmentSize = Gui.getPatientArray().get(index)
					.getRecords().size();
			Gui.getPatientArray().get(index).getRecords()
					.get(treatmentSize - 1).generateInvoice();
		}
		NURSELOGGER.log(Level.CONFIG, "Nurse " + Fn + " " + Ln
				+ " generated an invoice for Patient: " + first + " " + last);
	}

	/**
	 * Method to add a new treatment record
	 * 
	 * @param first
	 *            first name of the patient
	 * @param last
	 *            last name of the patient
	 */
	public void addTreatementRecord(String first, String last) {
		final int index = lookup(first, last);
		if (index >= 0) {
			final Scanner scan = new Scanner(System.in);

			System.out.println("Enter the attending nurse");
			final String nurse = scan.nextLine();
			System.out.println("Enter the chief complaint");
			final String comp = scan.nextLine();
			System.out.println("Enter the vital signs");
			final String vital = scan.nextLine();
			System.out.println("Enter the diagnosis");
			final String diag = scan.nextLine();
			System.out.println("Enter appointment details");
			final String app = scan.nextLine();

			final TreatmentRecord record = new TreatmentRecord(nurse, comp,
					vital, diag, last, first, app);
			Gui.getPatientArray().get(index).newTreatmentRecord(record);
		} else {
			System.out.println("Patient not found");
		}
		NURSELOGGER
				.log(Level.CONFIG, "Nurse " + Fn + " " + Ln
						+ " added treatment records fro Patient: " + first
						+ " " + last);
	}

	/**
	 * Method to view appointments
	 */
	public void viewAppointment() {
		final int size = appointmentArray.size();
		for (int i = 0; i < size; i++) {
			// System.out.println(appointmentArray.get(i).getDate());
			// System.out.println(appointmentArray.get(i).getTime());
			System.out.println(appointmentArray.get(i).getDoctor());
		}
	}

}
